package com.moorror.codegenvel.view.window.codegen.tree;

import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.ActionGroup;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
import com.intellij.openapi.project.Project;
import com.moorror.codegenvel.common.dialog.MRDialogInputName;
import com.moorror.codegenvel.common.tree.MTTreeCell;
import com.moorror.codegenvel.common.tree.TreePanelMark;
import com.moorror.codegenvel.common.tree.enums.AddSubTreeNodeType;
import com.moorror.codegenvel.db.sqlite.dao.BaseDAO;
import com.moorror.codegenvel.db.sqlite.dao.impl.CodeGenSceneDaoImpl;
import com.moorror.codegenvel.db.sqlite.entity.AbEntity;
import com.moorror.codegenvel.db.sqlite.entity.CodeGenScene;
import com.moorror.codegenvel.view.component.CodeGenByVelocityComponent;
import com.moorror.codegenvel.view.component.TabContentRightShow;
import com.moorror.codegenvel.view.window.codegen.CodeGenLeft;
import org.apache.commons.lang3.tuple.Pair;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;

/**
 * @Author jiyanghuang
 * @Date 2024/4/11 23:14
 */
public class MTTreeCellCodeGenRoot implements MTTreeCell {

    private Project project;

    public MTTreeCellCodeGenRoot(Project project) {
        this.project = project;
    }


    @Override
    public String cellShow() {
        return "CodeGenRoot";
    }

    @Override
    public Icon iconSelected() {
        return AllIcons.Hierarchy.Subtypes;
    }

    @Override
    public Icon iconUnselected() {
        return AllIcons.Hierarchy.Subtypes;
    }

    @Override
    public AddSubTreeNodeType addSubTreeNodeType() {
        return AddSubTreeNodeType.DIALOG;
    }

    @Override
    public DefaultMutableTreeNode buildSubTreeNode(String jsonStr, TreePanelMark treePaneLMark) {
        MTTreeCellCodeGenScene mockScene = new MTTreeCellCodeGenScene(project, CodeGenScene.of(jsonStr));
        DefaultMutableTreeNode sequenceTreeNode = new DefaultMutableTreeNode(mockScene);
        return sequenceTreeNode;
    }

    @Override
    public void addSubTreeNodeDialog(JTree tree, DefaultMutableTreeNode mutabLeTreeNode, TreePanelMark treePaneLMark) {
        CodeGenLeft mockLeft = (CodeGenLeft) treePaneLMark;
        new MRDialogInputName("", "scene name", mockLeft.fetchProject(), (form) -> {
            CodeGenScene codeGenScene = AbEntity.of(CodeGenScene.class);
            codeGenScene.setName(form.getInputName().getText());
            return BaseDAO.checkExistAdd(codeGenScene, CodeGenSceneDaoImpl.getInstance(), CodeGenScene::getName, () -> {
                DefaultMutableTreeNode chiLdNode = buildSubTreeNode(form.getInputName().getText(), treePaneLMark);
                addSubTreeNode(tree, mutabLeTreeNode, treePaneLMark, chiLdNode);
                return Pair.of(true, null);
            });
        }).ofDialogShow();
    }

    @Override
    public void treeSelectionListener(JTree tree, DefaultMutableTreeNode mutableTreeNode, TreePanelMark treePanelMark) {
        TabContentRightShow tabContentRightShow = CodeGenByVelocityComponent.getInstance(project).findInstance(TabContentRightShow.class);
        tabContentRightShow.updatePanel(null);
    }


    @Override
    public ActionGroup rightClickActionGroup(JTree tree, DefaultMutableTreeNode mutableTreeNode, DefaultActionGroup defaultActionGroup, TreePanelMark treePanelMark) {
        return null;
    }

    @Override
    public void reloadSubLocal(TreePanelMark treePanelMark, DefaultMutableTreeNode mutableTreeNode) {

    }
}
